import Vue from "vue";
import VueRouter from "vue-router";
import Loading from "../views/Loading.vue";
import Index from "../views/Index.vue";
import Summary from "../views/Summary.vue";
import Agenda from "../views/Agenda.vue";
import FAQs from "../views/FAQs.vue";
import Reg from "../views/Reg.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "load",
    component: Loading,
  },
  {
    path: "/index",
    name: "index",
    component: Index,
    redirect: "/index/summary", //重定向到第一个子路由
    children: [
      {
        path: "/index/summary",
        name: "summary",
        component: Summary,
      },
      {
        path: "/index/agenda",
        name: "agenda",
        component: Agenda,
      },
      {
        path: "/index/faqs",
        name: "faqs",
        component: FAQs,
      },
    ],
  },
  {
    path: "/reg",
    component: Reg,
  },
];

const router = new VueRouter({
  routes,
  linkActiveClass: "active",
});

//全局路由守卫 应用场景：后台管理系统，必须登录才能跳转到主页面
// router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(from)
  // const tokenStr = localStorage.getItem('token')
  // if (to.path === '/home' && !tokenStr) {
  //   next('/login')
  // } else {
  //   next()
  // }

  // 方式一 页面跳转 滚动条置顶
  // // chrome
  // document.body.scrollTop = 0;
  // // firefox
  // document.documentElement.scrollTop = 0;
  // // safari
  // window.pageYOffset = 0;
  // next();
// });

// 方式二 页面跳转 滚动条置顶
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
  // chrome
  document.body.scrollTop = 0;
  // firefox
  document.documentElement.scrollTop = 0;
  // safari
  window.pageYOffset = 0;
});

export default router;
